package com.qf.index.gcrdo;


import cn.hutool.core.util.RandomUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import com.qf.index.gcrdo.httpclient.HttpClient;
import com.qf.index.gcrdo.httpclient.SmallTool;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;


@Slf4j
public class CheckPathWay {

    public static void main(String[] args) throws Exception {

        // 校验通路 通过网址 https://www.kegg.jp/ 这个网站也可以校验疾病 后续再看 2025年5月8日18:01:30
        // 药物，数字代号：1; 基因，数字代号：2; 通路，数字代号：3; 疾病，数字代号：4
        Db use = Db.use(DSFactory.get("localdb"));
        List<Entity> query = use.query("select * from word_and_code_set_map_new where type = '通路' and tongyi_back = '不是' and  check3 is null ");
        SmallTool.printMessError("查询到的数量："+query.size());
        // 帮我写一个带监控的线程池改造下面方法
        // 1. 线程池
        // 2. 监控
        // 3. 线程池的线程数
        // 4. 线程池的队列
        // 5. 线程池的线程数
        // 6. 线程池的队列
        // 7. 线程池的线程数
        // 8. 线程池的队列
        // 9. 线程池的线程数
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 100000, 0L, java.util.concurrent.TimeUnit.MILLISECONDS, new java.util.concurrent.LinkedBlockingQueue<Runnable>(1000), new ThreadPoolExecutor.CallerRunsPolicy());
        for (Entity entity : query) {
            executor.submit(() -> {
                String word = entity.getStr("word");
                SmallTool.printMessError("开始++"+word);
                String urlDo = "https://www.kegg.jp/kegg-bin/search_pathway_text?map=map&keyword=thyroid%20cancer&mode=1&viewImage=false";
                urlDo = urlDo.replace("thyroid%20cancer", URLEncoder.encode(word));
                System.out.println(urlDo);
                String s = null;
                try {
                    s = HttpClient.getHtmlDirectProxy(urlDo);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (StringUtil.isNotNull(s)) {
                    SmallTool.printMessError("更新成功++word+"+word);
                    if (s.contains(word)) {
                        SmallTool.printMess("匹配成功");
                    }else {
                        SmallTool.printMessError("匹配失败");
                    }
                    entity.set("check3", s);
                    try {
                        use.insertOrUpdate(entity, "id");
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                    int i = RandomUtil.randomInt(1, 3);
                    long 了 = i * 1000 + RandomUtil.randomInt(0, 1000);
                    try {
                        Thread.sleep(了);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
//                    SmallTool.printMessError("睡眠"+了+"毫秒");
                    SmallTool.printMess("__________________________________________________");
                }
            }) ;
        }
        executor.shutdown();

        // 修改后的监控逻辑
        while (!executor.isTerminated()) {
            int activeCount = executor.getActiveCount();
            long taskCount = executor.getTaskCount();
            long completedTaskCount = executor.getCompletedTaskCount();
            SmallTool.printMess("活跃线程: " + activeCount
                + " | 总任务: " + taskCount
                + " | 已完成: " + completedTaskCount
                + " | 队列剩余: " + executor.getQueue().size());

            Thread.sleep(5000);
        }
        SmallTool.printMess("线程池已完全终止");
        System.out.println("全部结束");


    }





}


